<template>
  <MainLayout>
    <div class="document-detail">
      <div v-if="document" class="document-container">
        <!-- 文档头部信息 -->
        <div class="document-header">
          <h1 class="document-title">{{ document.title }}</h1>
          <div class="document-meta">
            <router-link :to="`/categories/${document.category.slug}`" class="category-link">
              {{ document.category.name }}
            </router-link>
            <span class="meta-item">{{ document.created_at }}</span>
            <span class="meta-item">{{ document.views }} 次浏览</span>
          </div>
          <div class="document-tags" v-if="document.tags.length">
            <router-link
              v-for="tag in document.tags"
              :key="tag.id"
              :to="`/tags/${tag.slug}`"
              class="tag-link"
            >
              {{ tag.name }}
            </router-link>
          </div>
        </div>

        <!-- 文档内容 -->
        <div class="document-content markdown-body" v-html="renderedContent"></div>

        <!-- 文档目录 -->
        <div class="document-toc" v-if="toc.length">
          <h3 class="toc-title">目录</h3>
          <ul class="toc-list">
            <li v-for="item in toc" :key="item.id" :class="['toc-item', `level-${item.level}`]">
              <a :href="`#${item.id}`" class="toc-link">{{ item.text }}</a>
            </li>
          </ul>
        </div>
      </div>
      <div v-else class="loading-state">加载中...</div>
    </div>
  </MainLayout>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { marked } from 'marked'
import MainLayout from '../components/layout/MainLayout.vue'
import { getDocumentDetail, increaseDocumentViews } from '../api/document'

const route = useRoute()
const document = ref(null)
const toc = ref([])

// 渲染Markdown内容
const renderedContent = computed(() => {
  if (!document.value) return ''
  return marked(document.value.content)
})

// 解析目录
const parseToc = (content) => {
  const headings = content.match(/^#{1,6}\s+.+$/gm) || []
  return headings.map((heading, index) => {
    const level = heading.match(/^#+/)[0].length
    const text = heading.replace(/^#+\s+/, '')
    const id = `heading-${index}`
    return { id, level, text }
  })
}

// 获取文档详情
const fetchDocumentDetail = async () => {
  try {
    const res = await getDocumentDetail(route.params.slug)
    document.value = res.data
    toc.value = parseToc(document.value.content)
    // 增加浏览量
    await increaseDocumentViews(route.params.slug)
  } catch (error) {
    console.error('获取文档详情失败:', error)
  }
}

onMounted(() => {
  fetchDocumentDetail()
})
</script>

<style scoped>
.document-detail {
  padding: 20px 0;
  position: relative;
}

.document-container {
  max-width: 800px;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.document-header {
  margin-bottom: 40px;
}

.document-title {
  font-size: 32px;
  font-weight: bold;
  margin: 0 0 20px;
  color: #333;
}

.document-meta {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  font-size: 14px;
  color: #999;
}

.category-link {
  color: #409eff;
  text-decoration: none;
}

.document-tags {
  display: flex;
  gap: 10px;
}

.tag-link {
  display: inline-block;
  padding: 4px 8px;
  background-color: #f5f5f5;
  border-radius: 4px;
  color: #666;
  text-decoration: none;
  font-size: 12px;
  transition: background-color 0.3s;
}

.tag-link:hover {
  background-color: #e8e8e8;
}

.document-content {
  line-height: 1.8;
  color: #333;
}

.document-toc {
  position: fixed;
  top: 100px;
  right: 40px;
  width: 240px;
  max-height: calc(100vh - 140px);
  overflow-y: auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.toc-title {
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 16px;
  color: #333;
}

.toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc-item {
  margin-bottom: 8px;
}

.toc-link {
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.toc-link:hover {
  color: #409eff;
}

.level-1 { margin-left: 0; }
.level-2 { margin-left: 16px; }
.level-3 { margin-left: 32px; }
.level-4 { margin-left: 48px; }
.level-5 { margin-left: 64px; }
.level-6 { margin-left: 80px; }

.loading-state {
  text-align: center;
  padding: 40px;
  color: #999;
  font-size: 14px;
}

/* Markdown样式 */
:deep(.markdown-body) {
  font-size: 16px;
  line-height: 1.8;
}

:deep(.markdown-body h1),
:deep(.markdown-body h2),
:deep(.markdown-body h3),
:deep(.markdown-body h4),
:deep(.markdown-body h5),
:deep(.markdown-body h6) {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: bold;
  line-height: 1.25;
}

:deep(.markdown-body h1) { font-size: 2em; }
:deep(.markdown-body h2) { font-size: 1.5em; }
:deep(.markdown-body h3) { font-size: 1.25em; }
:deep(.markdown-body h4) { font-size: 1em; }
:deep(.markdown-body h5) { font-size: 0.875em; }
:deep(.markdown-body h6) { font-size: 0.85em; }

:deep(.markdown-body p) {
  margin-top: 0;
  margin-bottom: 16px;
}

:deep(.markdown-body code) {
  padding: 0.2em 0.4em;
  margin: 0;
  font-size: 85%;
  background-color: rgba(27, 31, 35, 0.05);
  border-radius: 3px;
}

:deep(.markdown-body pre) {
  padding: 16px;
  overflow: auto;
  font-size: 85%;
  line-height: 1.45;
  background-color: #f6f8fa;
  border-radius: 3px;
}

:deep(.markdown-body pre code) {
  padding: 0;
  margin: 0;
  font-size: 100%;
  word-break: normal;
  white-space: pre;
  background: transparent;
  border: 0;
}

:deep(.markdown-body blockquote) {
  padding: 0 1em;
  color: #6a737d;
  border-left: 0.25em solid #dfe2e5;
  margin: 0 0 16px 0;
}

:deep(.markdown-body ul),
:deep(.markdown-body ol) {
  padding-left: 2em;
  margin-top: 0;
  margin-bottom: 16px;
}

:deep(.markdown-body img) {
  max-width: 100%;
  box-sizing: content-box;
  background-color: #fff;
}

:deep(.markdown-body table) {
  display: block;
  width: 100%;
  overflow: auto;
  margin-top: 0;
  margin-bottom: 16px;
  border-spacing: 0;
  border-collapse: collapse;
}

:deep(.markdown-body table th) {
  font-weight: bold;
}

:deep(.markdown-body table th,
.markdown-body table td) {
  padding: 6px 13px;
  border: 1px solid #dfe2e5;
}

:deep(.markdown-body table tr) {
  background-color: #fff;
  border-top: 1px solid #c6cbd1;
}

:deep(.markdown-body table tr:nth-child(2n)) {
  background-color: #f6f8fa;
}
</style> 